package com.zrrd.zuo.slwindow;

import java.util.LinkedList;

/**
 * @author ywb
 * @Description:
 * @date 2023/4/19 16:48
 */
//数组找一段范围内最大的数输出
public class SlidingWindowMaxArray {
    public int[] maxSlidingWindow(int[] nums, int k) {
        if(nums == null || nums.length < k || k < 1){
            return  null;
        }
        //大->小
        LinkedList<Integer> qmax = new LinkedList<>();
        int[] res = new int[nums.length-k+1];
        int index = 0;
        for (int i = 0; i < nums.length; i++) {
            //可以放在比他大的数后或空
            while (!qmax.isEmpty() && nums[qmax.peekLast()] <= nums[i]){
                qmax.pollLast();
            }
            qmax.addLast(i);
            if(qmax.peekFirst() <= i - k){
                qmax.pollFirst();
            }
            if(i >= k - 1){
                res[index++] = nums[qmax.peekLast()];
            }
        }return res;
    }
}
